package edu.washburn.cm333.project.entities;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;


public class PropertyManager {
	public static final String PROPERTY_FILE_NAME = "overrides.txt";

	static public void setProperties(Map<String,String> map){
		try {
			PrintWriter pw = new PrintWriter(PROPERTY_FILE_NAME);
			for(String s : map.keySet()){
				pw.println(s+"="+map.get(s));
			}
			pw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	static public Map<String,String> readProperties(){
		Map<String,String> addedOrOverridenProperties = new HashMap<String,String>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(PROPERTY_FILE_NAME));
			while(br.ready()){
				String[] keyValue = br.readLine().split("=");
				addedOrOverridenProperties.put(keyValue[0], keyValue[1]);
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return addedOrOverridenProperties;
	}
	
	static public EntityManager getEntityManager(){
		EntityManagerFactory fact = Persistence.createEntityManagerFactory("CAFM", readProperties());
		return fact.createEntityManager(); 
	}
}
